package com.ujson.chat.client.command;

import com.ujson.chat.client.controller.CommandController;
import com.ujson.chat.client.session.ClientSession;
import com.ujson.chat.common.enums.ConsoleCommandType;
import io.netty.channel.Channel;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Scanner;

/**
 * <h3>ujson-parent</h3>
 * <p>控制台命令</p>
 *
 * @author JerryLiu  a1031757312@gmail.com
 * @version 1.0
 * @date 2021-11-22 21:20
 */
@Data
public abstract class ConsoleCommand<T> implements ClientCommand {

    @Autowired
    CommandController commandController;

    private String inputString;

    public abstract ConsoleCommandType getType();

    public abstract T getInput();

    protected String getInputString() {
        return this.inputString;
    }
    protected String setInputString(String inputString) {
        this.inputString = inputString;
        return this.inputString;
    }

    public abstract String getMatchPattern();

    public boolean checkCommand(String input) {
        return StringUtils.isNotBlank(input) && input.matches(getMatchPattern());
    }

    public void exec(Scanner scanner) {
        while (!checkCommand((inputString = scanner.next()))) {
            System.out.println("非法登录输入, 请重新输入:");
        }
    }

    public ClientSession getClientSession() {
        return ClientSession.getClientSession(getChannel());
    }

    public Channel getChannel() {
        return commandController.getChannel();
    }

}
